<template>
	<view class="root">
		<view style="background-color: #fff;width: 100vw;"><user-header :userInfo="userInfo" :isGo="true"></user-header></view>
		<view class="my-plan">
			<view class="my-plan-content">
				<u-tabs :list="list" :is-scroll="false" :current="tabsCurrent" @change="tabsChange" active-color="#6D39E9" bg-color="#fff"></u-tabs>
			</view>
			<view class="no_login" v-if="!userInfo">
				<image src="http://love.q05.cc/40e3275365570062f9ba4726b761ad98.png" style="display: block;margin: 0 auto;margin-bottom: 50rpx;width: 431rpx;" mode=""></image>
				<view style="width: 377rpx;height: 58rpx;margin:0 auto;">
					<!-- #ifdef MP-WEIXIN -->
					<y-button  @click="loginClick" content="登录" styleValue="width: 377rpx;height:58rpx; font-size:25rpx;background: radial-gradient(168.4% 168.4% at 50% 50%, #6D39E9 0%, #DACAFC 100%); "></y-button>
					<!-- <button type="default" shape="circle" @click="loginClick" style="background: radial-gradient(168.4% 168.4% at 50% 50%, #6D39E9 0%, #DACAFC 100%);">登录</button> -->
					<!-- #endif -->
				</view>
			</view>
			<block v-if="userInfo">
				<view v-show="tabsCurrent === 0">
					<view class="clock-empty" v-if="isTodayEmpty">
						<empty-card-btn info="还没有计划书哦~" text="去添加" key_id="my-plan"></empty-card-btn>
					</view>
					<today-plan :todayPlanList="todayPlanList"></today-plan>
				</view>
				<view v-show="tabsCurrent === 1" style="margin-left: 58rpx;">
					<my-plan :data="myPlanList"></my-plan>
				</view>
				<view v-show="tabsCurrent === 2">
					<view v-for="(item, index) in communityList" :key="index"><Community-card :info="item" :isIndex="0"></Community-card></view>
				</view>
			</block>
		</view>
		<!-- <u-tabbar :list="vuex_tabbar" :mid-button="true"></u-tabbar> -->
	</view>
</template>

<script>
import todayPlan from './today-plan.vue';
import myPlan from './my-plan.vue';
import { Community } from '../../model/community.js';
import CommunityCard from '../../components/community-card/index.vue';
import { config } from '@/config/config.js';
import {Plan} from '@/model/plan.js'
let dayjs = require('dayjs')
export default {
	data() {
		return {
			list: [
				{
					name: '今日计划'
				},
				{
					name: '我的计划'
				},
				{
					name: '个人动态',
					count: 5
				}
			],
			isTodayEmpty:false,
			tabsCurrent: 0,
			communityList: [],
			userInfo: '',
			todayPlanList:[],
			myPlanList:[]
		};
	},
	components: {
		todayPlan,
		myPlan,
		CommunityCard
	},
	mounted() {
		const userInfo = uni.getStorageSync('userInfo');
		this.userInfo = userInfo;
		uni.showLoading({
			title:"加载中"
		})
		this.initList();
		this.initTodayPlan();
		this.initAllPlan();
		uni.hideLoading()
	},
	methods: {
		tabsChange(e) {
			this.tabsCurrent = e;
		},
		clickChangePlan() {
			console.log('修改计划');
		},
		async initList() {
			//初始化最新首页数据
			var paging = await Community.getMyContent();
			var data = await paging.getMoreData();
			if (!data.moreData) {
				this.status = 'nomore';
			}
			this.communityList = this.communityList.concat(data.items);
			this.communityObject = paging;
		},
		async initTodayPlan() {
			const planData = await Plan.getDatePlan(dayjs().format('YYYY-MM-DD'));
			// const plan = Plan.toTimeArray(planData);
			if (planData) {
				this.todayPlanList = planData
				this.isTodayEmpty=false;
			}else{
				this.isTodayEmpty=true;
			}
		},
		async initAllPlan(){
			const plan = await Plan.getMyAllPlan();
			if(plan) this.myPlanList=plan;
		},
		loginClick() {
			const that = this;
			uni.getUserProfile({
				desc: '用于完善个人资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
				success: res_1 => {
					uni.login({
						success(res_2) {
							if (res_2.code) {
								uni.showLoading({
									title: '登录中',
									mask: 'true'
								});
								//发起网络请求
								wx.request({
									url: `${config.apiBaseUrl}/login`,
									method: 'POST',
									data: {
										code: res_2.code,
										userName: res_1.userInfo.nickName,
										pictureUrl: res_1.userInfo.avatarUrl
									},
									success(loginRes) {
										that.userInfo = loginRes.data.data.user;
										uni.setStorageSync('userInfo', loginRes.data.data.user);
										uni.hideLoading();
										that.initTodayPlan();
										that.initList();
										that.initAllPlan();
										that.$u.vuex('fristLogin', true);
									},
									fail() {
										uni.hideLoading();
										uni.showToast({
											title: '服务器异常，稍等一下',
											icon: 'none'
										});
									}
								});
							} else {
								console.log('登录失败！' + res.errMsg);
							}
						}
					});
				}
			});
		}
	}
};
</script>

<style lang="scss">
page {
	background-color: #fff;
}
.my-plan {
	width: 100vw;
	min-height: 500rpx;
}
.my-plan-content {
	margin: 10rpx 0 40rpx 0;
}
.clock-empty{
	margin: 50rpx 0;
}
</style>
